package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.Bindable;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.NotifyParentCheckedChanged;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;

/**
 * Created by zxh on 16/12/6.
 */

public class SortModel extends ContextObservable implements NotifyParentCheckedChanged {
    public static final int EXPERT_SORT = 0;
    public static final int ASK_SORT = 1;
    //    private List<CheckedMenuItem> areaList;
    private List<String> selectedAreas;
    private ListBindingAdapter sortMenu;
    private ListBindingAdapter areas;

    @Bindable
    public ListBindingAdapter getSortMenu() {
        return sortMenu;
    }

    @Bindable
    public ListBindingAdapter getAreas() {
        return areas;
    }

    public void setAreaList(ListBindingAdapter<CheckedMenuItem> areaList) {
        initAreas(areaList);
    }

    public SortModel(Context context, int type, String sortCode, ArrayList<String> areas) {
        super(context);
        init(type, sortCode);
        selectedAreas = areas;
    }

    private void init(int type, String sortCode) {
        initSortMenu(type, sortCode);
    }

    private void initAreas(ListBindingAdapter<CheckedMenuItem> areaList) {
        Context context = getContext();
        areas = new ListBindingAdapter<CheckedMenuItem>(context, BR.menu);
        for (int i = 0; i < areaList.getCount(); i++) {
            CheckedMenuItem menu = areaList.getItem(i);
            CheckedMenuItem item = new CheckedMenuItem(context, R.layout.i_sort_menu,
                    menu.getTitle(), menu.getCode());
            item.setChecked(selectedAreas.contains(menu.getTitle()));
            areas.add(item);
        }
    }

    private void initSortMenu(int type, String sortCode) {
        Context context = getContext();
        sortMenu = new ListBindingAdapter<CheckedMenuItem>(context, BR.menu);
        if (type == ASK_SORT) {
            // 排序菜单列表
            CheckedMenuItem menuItem = new CheckedMenuItem(context, R.layout.i_sort_menu, "热度由高到低", "browser_up");
            menuItem.setChecked(menuItem.getCode().equals(sortCode));
            menuItem.setParent(this);
            sortMenu.add(menuItem);
            menuItem = new CheckedMenuItem(context, R.layout.i_sort_menu, "热度由低到高", "browser_down");
            menuItem.setChecked(menuItem.getCode().equals(sortCode));
            menuItem.setParent(this);
            sortMenu.add(menuItem);
            menuItem = new CheckedMenuItem(context, R.layout.i_sort_menu, "奖金由高到低", "money_up");
            menuItem.setChecked(menuItem.getCode().equals(sortCode));
            menuItem.setParent(this);
            sortMenu.add(menuItem);
            menuItem = new CheckedMenuItem(context, R.layout.i_sort_menu, "奖金由低到高", "money_down");
            menuItem.setChecked(menuItem.getCode().equals(sortCode));
            menuItem.setParent(this);
            sortMenu.add(menuItem);
        } else if (type == EXPERT_SORT) {
            // 排序菜单列表
            CheckedMenuItem menuItem = new CheckedMenuItem(context, R.layout.i_sort_menu, "好评由高到低", "judgerank_up");
            menuItem.setChecked(menuItem.getCode().equals(sortCode));
            menuItem.setParent(this);
            sortMenu.add(menuItem);
            menuItem = new CheckedMenuItem(context, R.layout.i_sort_menu, "好评由低到高", "judgerank_down");
            menuItem.setChecked(menuItem.getCode().equals(sortCode));
            menuItem.setParent(this);
            sortMenu.add(menuItem);
            menuItem = new CheckedMenuItem(context, R.layout.i_sort_menu, "费用由高到低", "money_up");
            menuItem.setChecked(menuItem.getCode().equals(sortCode));
            menuItem.setParent(this);
            sortMenu.add(menuItem);
            menuItem = new CheckedMenuItem(context, R.layout.i_sort_menu, "费用由低到高", "money_down");
            menuItem.setChecked(menuItem.getCode().equals(sortCode));
            menuItem.setParent(this);
            sortMenu.add(menuItem);
        }
    }

    public void onClickFinish(View view) {
        Activity activity = (Activity) getContext();
        String sort = "";
        String selectedArea = "";
        for (int i = 0; i < this.sortMenu.getCount(); i++) {
            CheckedMenuItem sortMenu = (CheckedMenuItem) this.sortMenu.getItem(i);
            if (sortMenu.isChecked()) {
                sort = sortMenu.getCode();
            }
        }

        for (int i = 0; i < areas.getCount(); i++) {
            CheckedMenuItem area = (CheckedMenuItem) areas.getItem(i);
            if (area.isChecked()) {
                selectedArea += area.getTitle() + ",";
            }
        }
        if (selectedArea != null && selectedArea.length() > 0) {
            selectedArea = selectedArea.substring(0, selectedArea.length() - 1);
        }
        Intent intent = activity.getIntent();
        intent.putExtra("sort", sort);
        intent.putExtra("selectedArea", selectedArea);

        activity.setResult(Activity.RESULT_OK, intent);
        activity.finish();
    }

    public void onClickCancel(View view) {
        Activity activity = (Activity) getContext();
        activity.setResult(Activity.RESULT_CANCELED);
        activity.finish();
    }

    @Override
    public void notifyCheckedChanged(CheckedMenuItem menu) {
        if (menu.isChecked() == false)
            return;
        for (int i = 0; i < this.sortMenu.getCount(); i++) {
            CheckedMenuItem sortMenu = (CheckedMenuItem) this.sortMenu.getItem(i);
            if (sortMenu != menu && sortMenu.isChecked()) {
                sortMenu.setChecked(false);
            }
        }
    }
}
